package com.test.publicmethod;

import net.sf.json.JSONObject;
import com.test.request.GetRequest;
import com.test.request.PostRequest;
import static org.testng.Assert.assertEquals;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.util.List;


public class PublicHelp {

    GetRequest getreq = new GetRequest();
    PostRequest postreq;

    List<APICaseModel> caseList;

    @Test(dataProvider = "dp")
    public void f(APICaseModel caseModel) {
        doCase(caseModel);
    }

    public void doCase(APICaseModel caseModel) {
        for (int i = 0; i < caseModel.getStepModels().size(); i++) {
            APIStepModel apiStepModel = caseModel.getStepModels().get(i);
            if (apiStepModel.getAction().equals("get")) {
                //执行get请求
                String actual = getreq.get(apiStepModel.getUrl(), apiStepModel.getValue(),1);
                JSONObject object=JSONObject.fromObject(actual);
                object.remove("timeStamp");
                assertEquals(object.toString(), apiStepModel.getExpect());
                /*
                            字符串比较通过assertEquals即可断言
                1.我们公司的接口返回值里面有一个时间戳，每次运行值都不一样，
                  所以我们需要自己封装一个函数进行字符串对比
                assertEquals(actual, apiStepModel.getExpect());
                2.由上所示，我们需要通过key找到时间戳的值把它置成null，然后再对比
                */

            } else if (apiStepModel.getAction().equals("post")) {
                //执行post请求
                String actual = postreq.post(apiStepModel.getUrl(), apiStepModel.getValue(),1);
            } else {
                System.err.println("----------------------");
            }

        }

    }


    @DataProvider
    public Object[] dp() {
		caseList = APICaseModel.getCaseList("D:/apitest.xlsx");
        // 获取用例
        int size = caseList.size();
        Object[] objects = new Object[size];
        for (int i = 0; i < objects.length; i++) {
            objects[i] = caseList.get(i);
        }
        return objects;
    }

}

